//
//  WelcomeBackViewController.m
//  SinaWeibo
//
//  Created by Apple on 16/8/12.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "WelcomeBackViewController.h"
#import "MainViewController.h"



@interface WelcomeBackViewController ()


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImgViewConsY;

@property (weak, nonatomic) IBOutlet UIButton *welcomeBtn;

@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;

@property (weak, nonatomic) UIImage *image;


@end

@implementation WelcomeBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置头像图片
 /*
    AcountModel *acountModel = [AcountModel acountModelFromSandbox];
    
    NSURL *imgURL = [NSURL URLWithString:acountModel.profile_image_url];
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:imgURL
                          options:0
                         progress:nil
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                            if (image) {
                                
                                self.image = image;
                            }
                        }];
    UIGraphicsBeginImageContextWithOptions(self.image.size, NO, 0.0);
    
    UIBezierPath *bPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.image.size.width/2,self.image.size.height/2) radius:MIN(self.image.size.width/2, self.image.size.height/2) startAngle:0 endAngle:M_PI*2 clockwise:YES];
    
    [bPath stroke];
    
    [bPath addClip];
    
    
    
    [self.image drawAtPoint:CGPointZero];
    
    UIImage *getIMG = UIGraphicsGetImageFromCurrentImageContext();
    
    self.iconImgView.image = getIMG;
    
    
    UIGraphicsEndImageContext();
    */
    
    
    self.iconImgView.layer.cornerRadius = 50;

    [self.iconImgView.layer masksToBounds];
    
    // 2.加载头像
    AcountModel  *account = [AcountModel acountModelFromSandbox];
    NSURL *iconURL = [NSURL URLWithString:account.profile_image_url];
    // 2.1获取SDWebImage管理对象
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    // 2.2利用管理对象下载图片
    [manager downloadImageWithURL:iconURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        if (finished) {
            //            DDLogDebug(@"cacheType = %tu", cacheType);
            self.iconImgView.image = image;
        }
    }];
    
    
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    

    
    self.welcomeBtn.alpha = 0;
    
    //头像上移
    [UIView animateWithDuration:0.5 animations:^{
        self.iconImgViewConsY.constant += 200;
        [self.view layoutIfNeeded];
    }completion:^(BOOL finished) {
        //完成后弹出欢迎回来
        [UIView animateWithDuration:0.5 animations:^{
            self.welcomeBtn.alpha = 1;
            [self.view layoutIfNeeded];
        }completion:^(BOOL finished) {
            //弹完欢迎回来 切换窗口主控制器为主界面。
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            
            MainViewController *mainVC = [[MainViewController alloc]init];
            
            window.rootViewController = mainVC;
        }];
    }];

    [self setProfileImage];
    
}

-(void)setProfileImage{
    

//    AcountModel *acountModel = [AcountModel acountModelFromSandbox];
//    
//    NSURL *iconImageURL = [NSURL URLWithString:acountModel.profile_image_url];
//    
//    [self.iconImgView sd_setImageWithURL:iconImageURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];

    
}





@end
